package com.goldcn.order.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goldcn.common.CommonTools;
import com.goldcn.common.DateUtil;
import com.goldcn.common.HttpUtil;
import com.goldcn.common.ListUtils;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.model.DataResult;
import com.goldcn.config.BankbaseCfg;
import com.goldcn.config.ProConstants;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.enumeration.TopDeptEnum;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.order.dao.OrderInfoMapper;
import com.goldcn.order.dto.BranchReportFormDto;
import com.goldcn.order.dto.MemberReportDto;
import com.goldcn.order.vo.BranchReportVo;
import com.goldcn.order.vo.MemberSalesVo;
import com.google.common.base.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/12.
 */
@Service
@ConfigurationProperties("bank")
public class OrderReportFormService {
    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    RestTemplate restTemplate;

    //用户数据库表配置
    private String databaseTableUser;

    public String getDatabaseTableUser() {
        return databaseTableUser;
    }

    public void setDatabaseTableUser(String databaseTableUser) {
        this.databaseTableUser = databaseTableUser;
    }

    @Autowired
    //private Logger logger = Logger.getLogger(OrderReportFormService.class);
    private static Logger logger = LoggerFactory.getLogger(OrderReportFormService.class);


    /**
     * 报表第一个部门的统计
     *
     * @param branchReportFormDto
     * @return
     */
    public Object getOrderListPortHeader(BranchReportFormDto branchReportFormDto) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<BranchReportVo> list = null;

        if (branchReportFormDto.getSearchName() != null && !"".equals(branchReportFormDto.getSearchName())) {
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getOrgListByLikeName + branchReportFormDto.getOrganizationalId() + "/" + branchReportFormDto.getSearchName(), restTemplate);
            List<Long> orgList = (List<Long>) dataResult.getData();
            if (orgList != null && orgList.size() != 0) {
                branchReportFormDto.setSearchIdList(orgList);
            } else {
                map.put("list", new PageInfo(list));
                map.put("pageCounts", 0);
                return map;
            }
        }

        int roleType = branchReportFormDto.getRoleType();

        //设置分页处理
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        switch (roleType) {
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.reportHeader(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportHeaderTwo(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportHeaderThir(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        List<Long> idList = new ArrayList<>(); //查询出来的id作为一个集合 查询出名字
        //查询出来不会为空 只会数据为0
        if (list != null && list.size() != 0) {
            for (BranchReportVo vo : list) {
                if ("0".equals(vo.getOrganizationId())) {
                    vo.setOrganizationId(branchReportFormDto.getOrganizationalId() + "");
                }
                idList.add(Long.parseLong(vo.getOrganizationId()));
            }
            logger.info("开始访问远程base数据");
            DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchListAllState, idList, restTemplate);
            if (dataResult.getIsSuccess() != 1) {
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
            }
            Map<String, Object> nameMap = (Map<String, Object>) dataResult.getData();
            for (BranchReportVo vo : list) {
                String benDepart = branchReportFormDto.getOrganizationalId() + "";
                if (benDepart.equals(vo.getOrganizationId())) {
                    vo.setOrganizationName(nameMap.get(vo.getOrganizationId()) + "(本部)");
                    vo.setIsSelf("1");
                } else {
                    vo.setOrganizationName((String) nameMap.get(vo.getOrganizationId()));
                    vo.setIsSelf("0");
                }
                vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
                vo.setPath((String) nameMap.get(vo.getOrganizationId() + "_path"));
                vo.setHasNext((String) nameMap.get(vo.getOrganizationId() + "_hasNext"));
            }

            PageInfo<List<BranchReportVo>> page = new PageInfo(list);
            map.put("list", page);
            map.put("pageCounts", page.getTotal());
        } else {
            map.put("list", new PageInfo(list));
            map.put("pageCounts", 0);
        }
        return map;
    }

    public Object getOrderListPortHeaderExport(BranchReportFormDto branchReportFormDto) {
        List<BranchReportVo> list = new ArrayList();
        if (branchReportFormDto.getSearchName() != null && !"".equals(branchReportFormDto.getSearchName())) {
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getOrgListByLikeName + branchReportFormDto.getOrganizationalId() + "/" + branchReportFormDto.getSearchName(), restTemplate);
            List<Long> orgList = (List<Long>) dataResult.getData();
            if (orgList != null && orgList.size() != 0) {
                branchReportFormDto.setSearchIdList(orgList);
            } else {
                return list;
            }
        }

        int roleType = branchReportFormDto.getRoleType();

        //设置分页处理
        switch (roleType) {
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.reportHeader(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportHeaderTwo(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportHeaderThir(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        List<Long> idList = new ArrayList<>(); //查询出来的id作为一个集合 查询出名字
        //查询出来不会为空 只会数据为0
        if (list != null && list.size() != 0) {
            for (BranchReportVo vo : list) {
                if ("0".equals(vo.getOrganizationId())) {
                    vo.setOrganizationId(branchReportFormDto.getOrganizationalId() + "");
                }
                idList.add(Long.parseLong(vo.getOrganizationId()));
            }
            logger.info("开始访问远程base数据");
            DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchList, idList, restTemplate);
            if (dataResult.getIsSuccess() != 1) {
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
            }
            Map<String, Object> nameMap = (Map<String, Object>) dataResult.getData();
            for (BranchReportVo vo : list) {
                String benDepart = branchReportFormDto.getOrganizationalId() + "";
                if (benDepart.equals(vo.getOrganizationId())) {
                    vo.setOrganizationName(nameMap.get(vo.getOrganizationId()) + "(本部)");
                    vo.setIsSelf("1");
                } else {
                    vo.setOrganizationName((String) nameMap.get(vo.getOrganizationId()));
                    vo.setIsSelf("0");
                }
                vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
                vo.setPath((String) nameMap.get(vo.getOrganizationId() + "_path"));
                vo.setHasNext((String) nameMap.get(vo.getOrganizationId() + "_hasNext"));
            }
        }
        return list;
    }

    /**
     * 报表支部
     *
     * @param branchReportFormDto
     * @return
     */
    public Object getOrderListBranch(BranchReportFormDto branchReportFormDto) {
        Map<String, Object> map = new HashMap<String, Object>();//返回结果的map
        int roleType = branchReportFormDto.getRoleType();
        if (roleType != 1 && roleType != 2 && roleType != 3) {
            throw CommonTools.createException(ErrorCodeEnum.queryBranchFail);
        }
        List<BranchReportVo> list = new ArrayList<>();
        //设置分页处理
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        switch (roleType) {
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.reportHeadquarters(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportBranch(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportTwoLevelBranchID(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }


        //此时我已经得到了各个部门的统计数据了 还欠缺一个名字
        List<Long> idList = new ArrayList<>();
        for (BranchReportVo vo : list) {
            this.getPortData(vo, roleType, branchReportFormDto);
            idList.add(Long.parseLong(vo.getOrganizationId()));
        }
        if (idList == null) {
            map.put("list", new PageInfo(list));
            map.put("pageCounts", 0);
        }
        DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchListAllState, idList, restTemplate);
        Map<String, Object> nameMap = (Map<String, Object>) dataResult.getData();
        for (BranchReportVo vo : list) {
            String benDepart = branchReportFormDto.getOrganizationalId() + "";
            if (benDepart.equals(vo.getOrganizationId())) {
                if (roleType == 1) {
                    vo.setOrganizationName(nameMap.get(vo.getOrganizationId()) + "(本部)");
                } else {
                    vo.setOrganizationName(nameMap.get(vo.getOrganizationId()) + "");
                }

            } else {
                vo.setOrganizationName((String) nameMap.get(vo.getOrganizationId()));
            }
            vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
            vo.setIsSelf("1");
            vo.setPath((String) nameMap.get(vo.getOrganizationId() + "_path"));
        }

        PageInfo<List<BranchReportVo>> page = new PageInfo(list);

        map.put("list", page);
        map.put("pageCounts", page.getTotal());
        return map;
    }

    public Object getOrderListBranchExport(BranchReportFormDto branchReportFormDto) {
        int roleType = branchReportFormDto.getRoleType();
        if (roleType != 1 && roleType != 2 && roleType != 3) {
            throw CommonTools.createException(ErrorCodeEnum.queryBranchFail);
        }
        List<BranchReportVo> list = new ArrayList();
        //设置分页处理
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        switch (roleType) {
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.reportHeadquarters(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportBranch(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportTwoLevelBranchID(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }

        //此时我已经得到了各个部门的统计数据了 还欠缺一个名字
        List<Long> idList = new ArrayList();
        for (BranchReportVo vo : list) {
            this.getPortData(vo, roleType, branchReportFormDto);
            idList.add(Long.parseLong(vo.getOrganizationId()));
        }
        DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchList, idList, restTemplate);
        Map<String, Object> nameMap = (Map<String, Object>) dataResult.getData();
        for (BranchReportVo vo : list) {
            String benDepart = branchReportFormDto.getOrganizationalId() + "";
            if (benDepart.equals(vo.getOrganizationId())) {
                if (roleType == 1) {
                    vo.setOrganizationName(nameMap.get(vo.getOrganizationId()) + "(本部)");
                } else {
                    vo.setOrganizationName(nameMap.get(vo.getOrganizationId()) + "");
                }
            } else {
                vo.setOrganizationName((String) nameMap.get(vo.getOrganizationId()));
            }
            vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
            vo.setIsSelf("1");
            vo.setPath((String) nameMap.get(vo.getOrganizationId() + "_path"));
        }
        return list;
    }

    /**
     * 得到销售人员的统计 2.0
     *
     * @param memberReportDto
     * @return
     */
    public Object getMemberSalesReport(MemberReportDto memberReportDto) {
        if (memberReportDto.getSearchName() != null && !"".equals(memberReportDto.getSearchName())) {
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName + memberReportDto.getOrganizationalId() + "/" + memberReportDto.getSearchName(), restTemplate);
            List<Long> saleManagerList = (List<Long>) dataResult.getData();
            if (saleManagerList != null && saleManagerList.size() != 0) {
                memberReportDto.setSearchIdList(saleManagerList);
            } else {
                memberReportDto.setSearchIdList(null);
                return new ArrayList<>();
            }

        }
        Map<String, Object> map = new HashMap<String, Object>();
        int roleType = memberReportDto.getRoleType();
        List<MemberSalesVo> list = new ArrayList<>();
        MemberSalesVo memberSalesVo = new MemberSalesVo();
        //封装page
        PageHelper.startPage(memberReportDto.getPageNo(), memberReportDto.getPageSize());
        //得到符合要求的
        switch (roleType) {
            case 1:
                memberReportDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.memberSalesCountsHeaders(memberReportDto);//修改
                break;
            case 2:
                list = orderInfoMapper.memberSalesCountsBranch(memberReportDto);
                break;
            case 3:
                list = orderInfoMapper.memberSalesCountsTwoLevel(memberReportDto);
                break;
            case 4:
                list = orderInfoMapper.memberSalesCountsSubbranchID(memberReportDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryMemberDataFail);
        }
        int index = -1;
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        //查询出来没有数据
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSaleId() == null || 0 == list.get(i).getSaleId() || "".equals(list.get(i).getSaleId())) {
                    memberSalesVo = list.get(i);
                    memberSalesVo.setSaleName("总部直属会员");
                    memberSalesVo.setOrganizationName("总部");
                    memberSalesVo.setContact("");
                    index = i;
                } else {
                    saleManagerList.add((list.get(i)).getSaleId());
                }
            }
        } else {
            //如果为空 则是直接返回了
            PageInfo<List<BranchReportVo>> page = new PageInfo(list);
            map.put("list", page);
            map.put("pageCounts", 0);
            return map;
        }
        //移除那条数据
        if (index != -1) {
            list.remove(index);
        }
        //如果为空 则表示是有一条数据或者没有数据
        if (saleManagerList.size() == 0) {
            if (index == -1) {
                //表示没有数据
                PageInfo<List<BranchReportVo>> page = new PageInfo(list);
                map.put("list", page);
                map.put("pageCounts", 0);
            } else {
                //表示只有一条数据
                list.add(memberSalesVo);
                PageInfo<List<BranchReportVo>> page = new PageInfo(list);
                map.put("list", page);
                map.put("pageCounts", 1);
            }
            return map;
        }
        //封装那些其他的字段 需要调用其他的服务 到这里了 那么表示传过去的数据肯定是有出了总部直属会员以外其他值的 肯定是有数据的
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE + "/userinfo/userInfoMain", saleManagerList, restTemplate);
        Map<Long, Map<String, Object>> userInfoMap = (Map<Long, Map<String, Object>>) dataResult.getData();
        list = this.getMapUserInfo(userInfoMap, list);
        if (index != -1) {
            list.add(index, memberSalesVo);
        }
        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        map.put("list", page);
        map.put("pageCounts", page.getTotal());
        return map;
    }

    public Object getMemberSalesReportExport(MemberReportDto memberReportDto) {
        if (memberReportDto.getSearchName() != null && !"".equals(memberReportDto.getSearchName())) {
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName + memberReportDto.getOrganizationalId() + "/" + memberReportDto.getSearchName(), restTemplate);
            List<Long> saleManagerList = (List<Long>) dataResult.getData();
            if (saleManagerList != null && saleManagerList.size() != 0) {
                memberReportDto.setSearchIdList(saleManagerList);
            } else {
                memberReportDto.setSearchIdList(null);
                return new ArrayList<>();
            }

        }
        int roleType = memberReportDto.getRoleType();
        List<MemberSalesVo> list = new ArrayList();
        MemberSalesVo memberSalesVo = new MemberSalesVo();
        //得到符合要求的
        switch (roleType) {
            case 1:
                memberReportDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.memberSalesCountsHeaders(memberReportDto);//修改
                break;
            case 2:
                list = orderInfoMapper.memberSalesCountsBranch(memberReportDto);
                break;
            case 3:
                list = orderInfoMapper.memberSalesCountsTwoLevel(memberReportDto);
                break;
            case 4:
                list = orderInfoMapper.memberSalesCountsSubbranchID(memberReportDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryMemberDataFail);
        }
        int index = -1;
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        //查询出来没有数据
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSaleId() == null || 0 == list.get(i).getSaleId() || "".equals(list.get(i).getSaleId())) {
                    memberSalesVo = list.get(i);
                    memberSalesVo.setSaleName("总部直属会员");
                    memberSalesVo.setOrganizationName("总部");
                    memberSalesVo.setContact("");
                    index = i;
                } else {
                    saleManagerList.add((list.get(i)).getSaleId());
                }
            }
        } else {
            //如果为空 则是直接返回了
            return list;
        }
        //移除那条数据
        if (index != -1) {
            list.remove(index);
        }
        //如果为空 则表示是有一条数据或者没有数据
        if (saleManagerList.size() == 0) {
            return list;
        }
        //封装那些其他的字段 需要调用其他的服务 到这里了 那么表示传过去的数据肯定是有出了总部直属会员以外其他值的 肯定是有数据的
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE + "/userinfo/userInfoMain", saleManagerList, restTemplate);
        Map<Long, Map<String, Object>> userInfoMap = (Map<Long, Map<String, Object>>) dataResult.getData();
        list = this.getMapUserInfo(userInfoMap, list);
        if (index != -1) {
            list.add(index, memberSalesVo);
        }
        return list;
    }

    /**
     * 得到销售总额和销售数量
     *
     * @param branchReportFormDto
     * @return
     */
    public Object getAllCounts(BranchReportFormDto branchReportFormDto) {
        branchReportFormDto.setStartTime(DateUtil.startOfTodDay());
        branchReportFormDto.setEndTime(DateUtil.endOfTodDay());
        return getAllCountsV2(branchReportFormDto);
    }

    public Object getAllCountsV2(BranchReportFormDto branchReportFormDto) {
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        Map<String, Object> map = orderInfoMapper.getAllCounts(branchReportFormDto);


       /* if (map == null || map.entrySet() == null) {
            map = new HashMap<>();
            map.put("amounts", 0);
            map.put("counts", 0);
            map.put("toDayAmounts", 0);// 今日销售金额
            map.put("toDayCounts", 0);//今日销售数量
            map.put("toDayOrderCount", 0);//今日订单数量
        }*/

        map.put("roleType", branchReportFormDto.getRoleType());
        map.put("organizationalId", branchReportFormDto.getOrganizationalId());
        return map;
    }

    //================================some tools method===================================

    /**
     * 封装人员信息
     *
     * @param userInfoMap
     * @param list
     * @return
     */
    public static List<MemberSalesVo> getMapUserInfo(Map<Long, Map<String, Object>> userInfoMap, List<MemberSalesVo> list) {
        if (list == null || list.size() == 0 || userInfoMap == null) {
            throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        for (MemberSalesVo memberSalesVo : list) {
            System.out.println(memberSalesVo.getSaleId() + "");

            Map<String, Object> userBrandMap = userInfoMap.get(memberSalesVo.getSaleId() + "");

            int level = (int) userBrandMap.get("level");
            String headerName = (String) userBrandMap.get("headerName");
            String branchName = (String) userBrandMap.get("branchName");
            String twoLevelBranchName = (String) userBrandMap.get("twoLevelBranchName");
            String subbranchName = (String) userBrandMap.get("subbranchName");
            memberSalesVo.setSaleName((String) userBrandMap.get("name"));
            memberSalesVo.setContact((String) userBrandMap.get("mobile"));
            switch (level) {
                case 1:
                    memberSalesVo.setOrganizationName(headerName);
                    break;
                case 2:
                    memberSalesVo.setOrganizationName(headerName + "/" + branchName);
                    break;
                case 3:
                    memberSalesVo.setOrganizationName(headerName + "/" + branchName + "/" + twoLevelBranchName);
                    break;
                case 4:
                    memberSalesVo.setOrganizationName(headerName + "/" + branchName + "/" + twoLevelBranchName + "/" + subbranchName);
                    break;
                default:
                    break;
            }
        }
        return list;
    }


    //===============================================================

    /**
     * 分部会员数据分析
     *
     * @param vo
     * @param roleType
     * @param branchReportFormDto
     */
    public static void getPortData(BranchReportVo vo, int roleType, BranchReportFormDto branchReportFormDto) {
        //这里是区分开始那个等级 并且取值
        switch (roleType) {
            case 1:
                if ("0".equals(vo.getBranchID()) && "0".equals(vo.getTwoLevelBranchID()) && "0".equals(vo.getSubbranchID())) {
                    //表示是总部的数据
                    vo.setOrganizationId(branchReportFormDto.getOrganizationalId() + "");
                    vo.setOrganizationName(branchReportFormDto.getOrganizationName());
                }
                if ((!"0".equals(vo.getBranchID())) && "0".equals(vo.getTwoLevelBranchID()) && "0".equals(vo.getSubbranchID())) {
                    //表示这个是分行的数据
                    vo.setOrganizationId(vo.getBranchID());
                }
                if ((!"0".equals(vo.getBranchID())) && (!"0".equals(vo.getTwoLevelBranchID())) && "0".equals(vo.getSubbranchID())) {
                    //表示这个是二级分行的数据
                    vo.setOrganizationId(vo.getTwoLevelBranchID());
                }
                if ((!"0".equals(vo.getBranchID())) && (!"0".equals(vo.getTwoLevelBranchID())) && (!"0".equals(vo.getSubbranchID()))) {
                    //表示这个是分行的数据
                    vo.setOrganizationId(vo.getSubbranchID());
                }
                break;
            case 2:
                if ("0".equals(vo.getTwoLevelBranchID()) && "0".equals(vo.getSubbranchID())) {
                    //表示是分行的数据
                    vo.setOrganizationId(branchReportFormDto.getOrganizationalId() + "");
                    vo.setOrganizationName(branchReportFormDto.getOrganizationName());
                }
                if ((!"0".equals(vo.getTwoLevelBranchID())) && "0".equals(vo.getSubbranchID())) {
                    //表示是二级分行的数据
                    vo.setOrganizationId(vo.getTwoLevelBranchID());
                }
                if ((!"0".equals(vo.getTwoLevelBranchID())) && (!"0".equals(vo.getSubbranchID()))) {
                    //表示是支行的数据
                    vo.setOrganizationId(vo.getSubbranchID());
                }
                break;
            case 3:
                if ("0".equals(vo.getSubbranchID())) {
                    //表示是二级分行的数据
                    vo.setOrganizationId(vo.getTwoLevelBranchID());
                }
                if (!"0".equals(vo.getSubbranchID())) {
                    //表示是支行的数据
                    vo.setOrganizationId(vo.getSubbranchID());
                }
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryBranchFail);
        }
    }

    public Object getSelfSalesData(BranchReportFormDto branchReportFormDto) {
        int roleType = branchReportFormDto.getRoleType();
        int pageSize = branchReportFormDto.getPageSize();
        List<MemberSalesVo> directlyList = null;
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        if (roleType == 1 && branchReportFormDto.getPageNo() == 1) {
            directlyList = orderInfoMapper.selectSelfSalesMemberOrder(branchReportFormDto);
            if (null != directlyList && null != directlyList.get(0) && directlyList.size() > 0) {
                directlyList.get(0).setOrganizationName("总部");
                directlyList.get(0).setContact("");
                directlyList.get(0).setSaleName("总部直属会员");
                branchReportFormDto.setPageSize(pageSize - 1);
            }
        }
        //设置分页处理
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        List<MemberSalesVo> list = orderInfoMapper.selectSelfSalesOrder(branchReportFormDto);
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            saleManagerList.add((list.get(i)).getSaleId());
        }
        if (saleManagerList == null || saleManagerList.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            if (directlyList != null && null != directlyList.get(0) && directlyList.size() > 0) {
                PageInfo<List<BranchReportVo>> page = new PageInfo(directlyList);
                map.put("list", page);
                map.put("pageCounts", page.getTotal());
            } else {
                map.put("list", new PageInfo(new ArrayList()));
                map.put("pageCounts", 0);
            }
            return map;
        }
        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE + "/userinfo/userInfoMain", saleManagerList, restTemplate);
        Map<Long, Map<String, Object>> userInfoMap = (Map<Long, Map<String, Object>>) dataResult.getData();


        PageInfo<List<BranchReportVo>> page = null;
        list = this.getMapUserInfo(userInfoMap, list);
        if (directlyList != null && null != directlyList.get(0) && directlyList.size() > 0) {
            directlyList.addAll(list);
            page = new PageInfo(directlyList);
        } else {
            page = new PageInfo(list);
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", page);
        map.put("pageCounts", page.getTotal());
        return map;

    }

    public Object getSelfSalesDataExport(BranchReportFormDto branchReportFormDto) {
        int roleType = branchReportFormDto.getRoleType();
        int pageSize = branchReportFormDto.getPageSize();
        List<MemberSalesVo> directlyList = null;
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        if (roleType == 1 && branchReportFormDto.getPageNo() == 1) {
            directlyList = orderInfoMapper.selectSelfSalesMemberOrder(branchReportFormDto);
            if (null != directlyList && null != directlyList.get(0) && directlyList.size() > 0) {
                directlyList.get(0).setOrganizationName("总部");
                directlyList.get(0).setContact("");
                directlyList.get(0).setSaleName("总部直属会员");
                branchReportFormDto.setPageSize(pageSize - 1);
            }
        }
        List<MemberSalesVo> list = orderInfoMapper.selectSelfSalesOrder(branchReportFormDto);
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            saleManagerList.add((list.get(i)).getSaleId());
        }
        if (saleManagerList == null || saleManagerList.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            if (directlyList != null && null != directlyList.get(0) && directlyList.size() > 0) {
                PageInfo<List<BranchReportVo>> page = new PageInfo(directlyList);
                map.put("list", page);
                map.put("pageCounts", page.getTotal());
            } else {
                map.put("list", new PageInfo(new ArrayList()));
                map.put("pageCounts", 0);
            }
            return map;
        }
        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE + "/userinfo/userInfoMain", saleManagerList, restTemplate);
        Map<Long, Map<String, Object>> userInfoMap = (Map<Long, Map<String, Object>>) dataResult.getData();

        list = this.getMapUserInfo(userInfoMap, list);
        if (directlyList != null && null != directlyList.get(0) && directlyList.size() > 0) {
            directlyList.addAll(list);
            return directlyList;
        }
        return list;
    }

    /**
     * 根据名称查询销售经理的销售
     *
     * @param memberReportDto
     * @return
     */
    public Object saleOrderInfoReportByName(MemberReportDto memberReportDto) {
        List<MemberSalesVo> list = new ArrayList<>();
        System.out.println(ProConstants.getSaleManagerIdListByName + memberReportDto.getOrganizationalId() + "/" + memberReportDto.getSearchName());
        DataResult dataResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName + memberReportDto.getOrganizationalId() + "/" + memberReportDto.getSearchName(), restTemplate);
        List<Long> saleManagerList = (List<Long>) dataResult.getData();
        if (saleManagerList != null && saleManagerList.size() != 0) {
            memberReportDto.setSearchIdList(saleManagerList);
            list = orderInfoMapper.memberOrderSalesById(memberReportDto);
        }
        if (list.size() != 0) {
            DataResult dataResult2 = HttpUtil.httpPost(ProConstants.BASE + "/userinfo/userInfoMain", saleManagerList, restTemplate);
            Map<Long, Map<String, Object>> userInfoMap = new HashMap<>();
            userInfoMap = (Map<Long, Map<String, Object>>) dataResult2.getData();
            if (userInfoMap.size() != 0) {
                list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
            }
        }
        return list;


    }


    public Object searchOrderHeaderByName(BranchReportFormDto branchReportFormDto) {
        List<MemberSalesVo> list = new ArrayList<>();
        //getOrgListByLikeName
        DataResult dataResult = HttpUtil.httpGet(ProConstants.getOrgListByLikeName + branchReportFormDto.getOrganizationalId() + "/" + branchReportFormDto.getSearchName(), restTemplate);
        List<Long> orgList = (List<Long>) dataResult.getData();
        return null;
    }


    public Object getEmpMemberAllCounts(BranchReportFormDto branchReportFormDto) {
        DataResult dataResult = HttpUtil.httpGet(ProConstants.getEmpMembCountData
                + branchReportFormDto.getRoleType() + "/" + branchReportFormDto.getOrganizationalId(), restTemplate);
        if (dataResult.getIsSuccess() != 1) {
            throw new ServiceException("调用base服务获取用户会员&员工统计接口出错", RestConst.ErrorCode.SERVICE_FAIL);
        }
        Map<String, Object> map = (Map<String, Object>) dataResult.getData();
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        Map<String, Object> orderMap = orderInfoMapper.getAllCounts(branchReportFormDto);
        map.put("amounts", orderMap.get("amounts"));
        map.put("counts", orderMap.get("counts"));
        return map;
    }

    public Object getBranchEmployee(BranchReportFormDto branchReportFormDto) {
//        DataResult dataResult = HttpUtil.httpGet(ProConstants.getBranchEmployee
//                + (branchReportFormDto.getLevel()) + "/" +
//                branchReportFormDto.getOrganizationalId(), DataResult.class);
//        if (dataResult.getIsSuccess() != 1) {
//            throw new ServiceException("调用base服务获取用户分部员工数接口出错", RestConst.ErrorCode.SERVICE_FAIL);
//        }
//        List<Map<String, Object>> list = (List<Map<String, Object>>) dataResult.getData();
        branchReportFormDto.setDatabaseTable(databaseTableUser);
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        List list = orderInfoMapper.getBranchEmployee(branchReportFormDto);
        return list;
    }

    public Object getBranchMember(BranchReportFormDto branchReportFormDto) {
//        DataResult dataResult = HttpUtil.httpGet(ProConstants.getBranchMember
//                + (branchReportFormDto.getLevel()) + "/" +
//                branchReportFormDto.getOrganizationalId(), DataResult.class);
//        if (dataResult.getIsSuccess() != 1) {
//            throw new ServiceException("调用base服务获取用户分部会员数接口出错", RestConst.ErrorCode.SERVICE_FAIL);
//        }
//        List<Map<String, Object>> list = (List<Map<String, Object>>) dataResult.getData();

        branchReportFormDto.setDatabaseTable(databaseTableUser);
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        List list = orderInfoMapper.getBranchMember(branchReportFormDto);
        return list;
    }

    public Integer getEmployeeActNum(Integer level, Long oID) {
        DataResult dataResult = HttpUtil.httpGet(ProConstants.getEmployeeActNum
                + (level) + "/" +
                oID, restTemplate);
        if (dataResult.getIsSuccess() != 1) {
            throw new ServiceException("调用base服务获取用户分部员工数接口出错", RestConst.ErrorCode.SERVICE_FAIL);
        }
        Integer total = Integer.parseInt(dataResult.getData().toString());
        return total;
    }

    /**
     * 员工&会员排行
     *
     * @param branchReportFormDto
     * @return
     */
    public Object empMemberTop(BranchReportFormDto branchReportFormDto) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<BranchReportVo> list = null;
        branchReportFormDto.setDatabaseTable(databaseTableUser);
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        switch (branchReportFormDto.getRoleType()) {
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.empMemberTop(branchReportFormDto);
                break;
            case 2:
                branchReportFormDto.setLevelNext(branchReportFormDto.getRoleType() + 1);
                list = orderInfoMapper.empMemberTop_2(branchReportFormDto);
                break;
            case 3:
                branchReportFormDto.setLevelNext(branchReportFormDto.getRoleType() + 1);
                list = orderInfoMapper.empMemberTop_3(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }

        for (BranchReportVo vo : list) {
            if (!"1".equals(vo.getIsSelf()) || branchReportFormDto.getRoleType() > 1) {
                DataResult dataResult = HttpUtil.httpGet(ProConstants.getEmpMembCountData
                        + vo.getLevel() + "/" + vo.getOrganizationId(), restTemplate);
                if (dataResult.getIsSuccess() != 1) {
                    throw new ServiceException("调用base服务获取用户会员&员工统计接口出错", RestConst.ErrorCode.SERVICE_FAIL);
                }
                Map<String, Object> dataMap = (Map<String, Object>) dataResult.getData();
                vo.setTwoLevelBranchNum(Integer.parseInt(dataMap.get("twoLevelBranchNum").toString()));
                if (vo.getLevel() > 1 && "1".equals(vo.getIsSelf())) {
                    vo.setSubbranchNum(0);
                } else {
                    vo.setSubbranchNum(Integer.parseInt(dataMap.get("subbranchNum").toString()));
                }
            } else {//本部无支部数
                vo.setTwoLevelBranchNum(0);
                vo.setSubbranchNum(0);
            }

            Map<String, Object> totalMap = new HashMap<>();
            totalMap = orderInfoMapper.totalSalleCount(branchReportFormDto.getRoleType(), Long.valueOf(vo.getOrganizationId()),
                    null, branchReportFormDto.getStartTime(), branchReportFormDto.getEndTime(), SystemContext.getCompanyID());
            vo.setAmounts(Float.parseFloat(totalMap.get("amounts").toString()));//总销售额
            vo.setCounts(Integer.valueOf(totalMap.get("counts").toString()));//总销售数

            //注册员工总数=分部员工数+二级分部员工数+支部员工数
            vo.setEmployeeRegNum(vo.getEmployeeNum() + vo.getTwoBranchEmpNum() + vo.getSubbranchEmpNum());
            //会员注册数
            BranchReportFormDto memberDto = new BranchReportFormDto();
            memberDto.setOrganizationalId(Long.valueOf(vo.getOrganizationId()));
            memberDto.setLevel(vo.getLevel());
            List<Map<String, Object>> mlist = (List<Map<String, Object>>) getBranchMember(memberDto);
            Integer totalMemNum = 0;
            if ("0".equals(vo.getIsSelf())) {
                for (Map<String, Object> mMap : mlist) {
                    totalMemNum += Integer.parseInt(mMap.get("totalMemNum").toString());
                }
            } else {
                totalMemNum += Integer.parseInt(mlist.get(0).get("totalMemNum").toString());
            }
            vo.setMembereRegNum(totalMemNum);

            //员工激活数
            Integer employeeActNum = 0;
            if (Objects.equal(Long.valueOf(vo.getOrganizationId()), TopDeptEnum.valuekOf(SystemContext.getCompanyID(), restTemplate).getDeptID())) {//总部(本部)特殊处理
                employeeActNum = getEmployeeActNum(0, Long.valueOf(vo.getOrganizationId().toString()));
                //销售总额，总数
                Map<String, Object> countMap = orderInfoMapper.heardEmpMemberTop(SystemContext.getCompanyID());
                vo.setCounts(Integer.parseInt(countMap.get("counts").toString()));
                vo.setAmounts(Float.parseFloat(countMap.get("amounts").toString()));
                //员工，会员销售数
                Long employeeSalleCount = orderInfoMapper.heardEmployeeSalleCount(
                        branchReportFormDto.getStartTime(), branchReportFormDto.getEndTime(), TopDeptEnum.valuekOf(SystemContext.getCompanyID(), restTemplate).getDeptID());
                vo.setEmployeeSalleNum(Integer.parseInt(employeeSalleCount.toString()));
                //会员销售数=总销售数-员工销售数
                Integer counts = Integer.parseInt(countMap.get("counts").toString());
                Integer memberSalleNum = counts - Integer.parseInt(employeeSalleCount.toString());
                vo.setMemberSalleNum(memberSalleNum);
            } else {
                employeeActNum = getEmployeeActNum(vo.getLevel(), Long.valueOf(vo.getOrganizationId().toString()));
                Long employeeSalleCount = orderInfoMapper.employeeSalleCount
                        (vo.getLevel(), Long.valueOf(vo.getOrganizationId()), null,
                                branchReportFormDto.getStartTime(), branchReportFormDto.getEndTime(), SystemContext.getCompanyID());
                vo.setEmployeeSalleNum(Integer.parseInt(employeeSalleCount.toString()));
                Integer memberSalleNum = vo.getCounts() - Integer.parseInt(employeeSalleCount.toString());
                vo.setMemberSalleNum(memberSalleNum);
            }
            vo.setEmployeeActNum(employeeActNum);
            if (branchReportFormDto.getRoleType() > 1 && "1".equals(vo.getIsSelf())) {
                Long myActNum = orderInfoMapper.getEmployeeActNum(Long.valueOf(vo.getOrganizationId()));
                vo.setEmployeeActNum(myActNum.intValue());
            } else {
                vo.setEmployeeActNum(employeeActNum);
            }
        }

        if (null != branchReportFormDto.getOrderByField()//自定义排序
                && "" != branchReportFormDto.getOrderByField()
                && null != branchReportFormDto.getIsAsc()
                && "" != branchReportFormDto.getIsAsc()) {
            boolean asc;
            if ("0".equals(branchReportFormDto.getIsAsc())) {
                asc = false;
            } else {
                asc = true;
            }
            //true:asc false:desc
            ListUtils.sort(list, asc, branchReportFormDto.getOrderByField());
        } else {
            ListUtils.sort(list, false, "employeeRegNum");
        }

        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        map.put("list", page);
        map.put("pageCounts", list.size());
        return map;
    }


    /**
     * 员工统计-总部
     *
     * @param branchReportFormDto
     * @return
     */
    public Object getHeadEmployeeCount(BranchReportFormDto branchReportFormDto) {
        branchReportFormDto.setDatabaseTable(databaseTableUser);
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        int pageSize = branchReportFormDto.getPageSize();
        List<MemberSalesVo> directlyList = null;
        branchReportFormDto.setVersion(2);
        if (branchReportFormDto.getRoleType() == 1 && branchReportFormDto.getPageNo() == 1) {
            branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
            directlyList = orderInfoMapper.selectSelfSalesMemberOrder(branchReportFormDto);
            if (directlyList != null && directlyList.size() > 0) {
                directlyList.get(0).setOrganizationName("总部");
                directlyList.get(0).setContact("");
                directlyList.get(0).setSaleName("总部直属会员");
                branchReportFormDto.setPageSize(pageSize - 1);
            }
        }
        PageInfo<List<MemberSalesVo>> page = null;
        branchReportFormDto.setTopDeptID(TopDeptEnum.valuekOf(SystemContext.getCompanyID(), restTemplate).getDeptID());
        List<MemberSalesVo> list = orderInfoMapper.heardEmployee(branchReportFormDto);
        for (MemberSalesVo vo : list) {
            vo.setOrganizationName("总部");
            vo.setSaleId(vo.getUserID());
            vo.setSaleName(vo.getUserName());
            DataResult udataResult = HttpUtil.httpGet(ProConstants.getMemberNumBySalesManager
                    + vo.getUserID(), restTemplate);
            if (udataResult.getIsSuccess() != 1) {
                throw new ServiceException("调用base服务获取理财经理的会员注册数出错", RestConst.ErrorCode.SERVICE_FAIL);
            }
            vo.setMembereRegNum(Integer.parseInt(udataResult.getData().toString()));//会员注册数

            Long employeeSalleCount = orderInfoMapper.employeeSalleCount(null, null, vo.getUserID(),
                    branchReportFormDto.getStartTime(), branchReportFormDto.getEndTime(), SystemContext.getCompanyID());//本人购买数
            vo.setMySalesNum(Integer.parseInt(employeeSalleCount.toString()));

            Map<String, Object> totalMap = new HashMap<>();
            totalMap = orderInfoMapper.totalSalleCount(null, null, vo.getUserID(),
                    branchReportFormDto.getStartTime(), branchReportFormDto.getEndTime(), SystemContext.getCompanyID());
            vo.setAmounts(Float.parseFloat(totalMap.get("amounts").toString()));//总销售额
            vo.setCounts(Integer.valueOf(totalMap.get("counts").toString()));//总销售数
            //会员购买数=总销售数-本人购买数
            vo.setMembereSalesNum(vo.getCounts() - vo.getMySalesNum());
        }
        if (directlyList != null && directlyList.size() > 0) {
            directlyList.addAll(list);
            page = new PageInfo(directlyList);
        } else {
            page = new PageInfo(list);
        }
        if (null != branchReportFormDto.getSearchName() && !"".equals(branchReportFormDto.getSearchName()))
            directlyList.remove(0);//名称检索不包含本部
        if (null != branchReportFormDto.getOrderByField()//自定义排序
                && "" != branchReportFormDto.getOrderByField()
                && null != branchReportFormDto.getIsAsc()
                && "" != branchReportFormDto.getIsAsc()) {
            boolean asc;
            if ("0".equals(branchReportFormDto.getIsAsc())) {
                asc = false;
            } else {
                asc = true;
            }
            //true:asc false:desc
            ListUtils.sort(directlyList, asc, branchReportFormDto.getOrderByField());
        } else {
            ListUtils.sort(directlyList, false, "amounts");
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", page);
        map.put("pageCounts", page.getTotal());
        return map;
    }

    /**
     * 员工统计
     *
     * @param memberReportDto
     * @return
     */
    public Object getEmployeeCount(MemberReportDto memberReportDto) {
        if (memberReportDto.getSearchName() != null && !"".equals(memberReportDto.getSearchName())) {
            DataResult saleResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName + memberReportDto.getOrganizationalId() + "/" + memberReportDto.getSearchName(), restTemplate);
            List<Long> saleManagerList = (List<Long>) saleResult.getData();
            if (saleManagerList != null && saleManagerList.size() != 0) {
                memberReportDto.setSearchIdList(saleManagerList);
            } else {
                memberReportDto.setSearchIdList(null);
                return new ArrayList<>();
            }

        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<MemberSalesVo> list = new ArrayList<>();
        MemberSalesVo memberSalesVo = new MemberSalesVo();
        //封装page
        PageHelper.startPage(memberReportDto.getPageNo(), memberReportDto.getPageSize());
        //得到符合要求的
        switch (memberReportDto.getRoleType()) {
            case 1:
                memberReportDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.memberSalesCountsHeaders(memberReportDto);//修改
                break;
            case 2:
                list = orderInfoMapper.memberSalesCountsBranch(memberReportDto);
                break;
            case 3:
                list = orderInfoMapper.memberSalesCountsTwoLevel(memberReportDto);
                break;
            case 4:
                list = orderInfoMapper.memberSalesCountsSubbranchID(memberReportDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryMemberDataFail);
        }
        int index = -1;
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        //查询出来没有数据
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSaleId() == null || 0 == list.get(i).getSaleId() || "".equals(list.get(i).getSaleId())) {
                    memberSalesVo = list.get(i);
                    memberSalesVo.setSaleName("总部直属会员");
                    memberSalesVo.setOrganizationName("总部");
                    memberSalesVo.setContact("");
                    index = i;
                } else {
                    saleManagerList.add((list.get(i)).getSaleId());
                }
            }
        } else {
            //如果为空 则是直接返回了
            PageInfo<List<BranchReportVo>> page = new PageInfo(list);
            map.put("list", page);
            map.put("pageCounts", 0);
            return map;
        }
        //移除那条数据
        if (index != -1) {
            list.remove(index);
        }
        //如果为空 则表示是有一条数据或者没有数据
        if (saleManagerList.size() == 0) {
            if (index == -1) {
                //表示没有数据
                PageInfo<List<BranchReportVo>> page = new PageInfo(list);
                map.put("list", page);
                map.put("pageCounts", 0);
            } else {
                //表示只有一条数据
                list.add(memberSalesVo);
                PageInfo<List<BranchReportVo>> page = new PageInfo(list);
                map.put("list", page);
                map.put("pageCounts", 1);
            }
            return map;
        }
        //封装那些其他的字段 需要调用其他的服务 到这里了 那么表示传过去的数据肯定是有出了总部直属会员以外其他值的 肯定是有数据的
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE + "/userinfo/userInfoMain", saleManagerList, restTemplate);
        Map<Long, Map<String, Object>> userInfoMap = (Map<Long, Map<String, Object>>) dataResult.getData();
        list = this.getMapUserInfo(userInfoMap, list);
        if (index != -1) {
            list.add(index, memberSalesVo);
        }
        for (MemberSalesVo mVo : list) {
            DataResult udataResult = HttpUtil.httpGet(ProConstants.getMemberNumBySalesManager
                    + mVo.getSaleId(), restTemplate);
            if (udataResult.getIsSuccess() != 1) {
                throw new ServiceException("调用base服务获取理财经理的会员注册数出错", RestConst.ErrorCode.SERVICE_FAIL);
            }

        }
        if (null != memberReportDto.getOrderByField()//自定义排序
                && "" != memberReportDto.getOrderByField()
                && null != memberReportDto.getIsAsc()
                && "" != memberReportDto.getIsAsc()) {
            boolean asc;
            if ("0".equals(memberReportDto.getIsAsc())) {
                asc = false;
            } else {
                asc = true;
            }
            //true:asc false:desc
            ListUtils.sort(list, asc, memberReportDto.getOrderByField());
        } else {
            ListUtils.sort(list, false, "amounts");
        }
        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        map.put("list", page);
        map.put("pageCounts", page.getTotal());
        return map;
    }


    /**
     * 员工&会员排行
     *
     * @param branchReportFormDto
     * @return
     */
    public Object empMemberTopV2(BranchReportFormDto branchReportFormDto) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<BranchReportVo> list = null;
        branchReportFormDto.setDatabaseTable(databaseTableUser);
        PageHelper.startPage(branchReportFormDto.getPageNo(), branchReportFormDto.getPageSize());
        branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
        list = orderInfoMapper.empMemberTopV2(branchReportFormDto);
        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        map.put("list", page);
        map.put("pageCounts", list.size());
        return map;
    }

    /**
     * 会员统计
     *
     * @param memberReportDto
     * @return
     */
    public Object getMemberCountCount(MemberReportDto memberReportDto) {
        memberReportDto.setCompanyID(SystemContext.getCompanyID());
        memberReportDto.setDatabaseTable(databaseTableUser);
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(memberReportDto.getPageNo(), memberReportDto.getPageSize());
        List mList = orderInfoMapper.getMemberCountCount(memberReportDto);
        PageInfo<List> page = new PageInfo(mList);
        map.put("list", page);
        map.put("pageCounts", page.getTotal());
        return map;
    }


    /**
     * 员工统计
     *
     * @param memberReportDto
     * @return
     */
    public Object getEmployeeCountV2(MemberReportDto memberReportDto) {
        Map<String, Object> map = new HashMap<String, Object>();
        memberReportDto.setDatabaseTable(databaseTableUser);
        //封装page
        PageHelper.startPage(memberReportDto.getPageNo(), memberReportDto.getPageSize());
        List list = orderInfoMapper.memberSalesCountsV2(memberReportDto);
        PageInfo page = new PageInfo(list);
        map.put("list", page);
        map.put("pageCounts", page.getTotal());

        return map;
    }


}
